import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/merchant/jishipages/AddJiShiPage.dart';
class JiShiListPage extends StatefulWidget {
  JiShiListPage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _JiShiListPage createState() => _JiShiListPage();
}

class _JiShiListPage extends State<JiShiListPage>{
  List jishiList=[];    //技师集合
  String merchantId=""; //店铺id

  _JiShiListPage(){
    getJishi();
  }


  void getJishi() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    merchantId = prefs.getString("merchantID");
    var x = await HttpUtil().get("/waimai/merchantJishi/getJishiByMercahnt?mId="+merchantId);
    if(x['code']==200){
      jishiList.addAll(x['data']);
    }
    print(jishiList);
    if(mounted)
      setState(() {

      });
  }

  Widget showJishi(){
    List<Widget> x=[];
    for(int a=0;a<jishiList.length;a++){
      print(jishiList[a]);
      x.add(
          ListTile(
            leading: IconButton(icon: Icon(Icons.remove_circle_outline,color: Colors.red,), onPressed: (){
              showDialog<Null>(
                context: context,
                barrierDismissible: false,
                builder: (BuildContext context) {
                  return new AlertDialog(
                    title: new Text('删除该技师的信息'),
                    content: Text('确定删除?'),
                    actions: <Widget>[
                      new FlatButton(
                        child: new Text('取消'),
                        onPressed: (){
                          Navigator.of(context).pop();
                        },
                      ),
                      new FlatButton(
                          child: new Text('确定'),
                          onPressed: () async{
                            // 发起请求删除购物车
                            var x= await HttpUtil().get("waimai/merchantJishi/deleteJishi?id="+jishiList[a]['id']);
                            if(x['code']==200){// 如果删除成功
                              jishiList.removeAt(a);
                              a--;
                              Fluttertoast.showToast(
                                  msg: "删除成功",
                                  toastLength: Toast.LENGTH_SHORT,
                                  gravity: ToastGravity.BOTTOM,
                                  timeInSecForIosWeb: 1,
                                  backgroundColor: Colors.black45,
                                  textColor: Colors.white,
                                  fontSize: 16.0
                              );
                              Navigator.of(context).pop();
                              setState(() {

                              });
                            }
                          }
                      ),
                    ],
                  );
                },
              ).then((val) {
                print(val);
              });
            }),
            subtitle: Text(jishiList[a]['name']),
            title:Row(
              children: [
                CachedNetworkImage(imageUrl: Config.getInstance().getHost()+jishiList[a]['picture'],height: ScreenUtil().setHeight(70),width: ScreenUtil().setWidth(80),),
                SizedBox(width: 10,),
                Expanded(child: Text(jishiList[a]['jieshao'],style: TextStyle(fontSize: 14),)),
              ],
            ) ,
            tileColor: Colors.white,
            trailing: IconButton(icon: Icon(Icons.border_color), onPressed: () async {
             Map value = await MyRoute.push(AddJiShiPage(isEdit: true,jishi: jishiList[a],));
             if(value!=null){
               jishiList[a]['name']=value['name'];
               jishiList[a]['phone']=value['phone'];
               jishiList[a]['picture']=value['picture'];
               jishiList[a]['jieshao']=value['jieshao'];
               setState(() {

               });
             }
            }),
          )
      );
    }

    return Column(
      children: x,
    );
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 820) ;
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("技师管理")
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            GestureDetector(
              child: ListTile(
                leading: Icon(Icons.add,color: Colors.white,),
                title: Text("新增技师"),
                tileColor: Colors.red,
              ),
              onTap: () async {
                print("新增技师");
                Map value = await MyRoute.push(AddJiShiPage(isEdit: false,));
                if(value!=null){
                  jishiList.add(value);
                  setState(() {

                  });
                }
              },
            ),
            SizedBox(height: ScreenUtil().setHeight(20),),
            Container(
              child: showJishi(),
            )
          ],
        ),
      ),

    );
  }
}